package models;

import play.db.jpa.GenericModel;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/**
 * Created by xuyang on 2017/6/13.
 */
@Entity(name = "public.\"TB005\"")
public class TB005 extends GenericModel {

    @Id
    @Column(name = "\"AIRCRAFT_ID\"")
    public String aircraft_id;

    public String getId() {
        return aircraft_id;
    }

    @Override
    public Object _key() {
        return getId();
    }

    @ManyToOne
    @JoinColumn(name="\"AIRLINES_ID\"", nullable = true)
    public TB007 airlines;

    @ManyToOne(cascade = CascadeType.MERGE)
    @JoinColumn(name="\"AIRCRAFT_TYPE\"", nullable = true)
    public TB006 aircraft_type;

    @ManyToMany(cascade={CascadeType.PERSIST}, fetch = FetchType.LAZY)
    @JoinTable(name = "public.\"TA016\"",
            joinColumns = { @JoinColumn(name = "\"AIRCRAFT_ID\"") },
            inverseJoinColumns = { @JoinColumn(name = "\"SPLIT_TEMPLATE_ID\"") })//id分别为这两张表的主键
    public List<TA014> split_templates;

    public TB005(String aircraft_id) {
        this.aircraft_id = aircraft_id;
        this.split_templates = new ArrayList<>();
    }

    public void addSpliteTemplatesByIds(Iterable templateIds) {

        for (Object id: templateIds) {
            TA014 tmpl = TA014.findById(Long.valueOf(id.toString()));
            if (tmpl != null && !split_templates.contains(tmpl)) {
                split_templates.add(tmpl);
            }
        }
    }

    public void removeSlitTemplatesByIds(Iterable templateIds) {
        for (Object id: templateIds) {
            TA014 tmpl = TA014.findById(Long.valueOf(id.toString()));
            if (tmpl != null) {
                split_templates.remove(tmpl);
            }
        }
    }
}
